package com.ysy.tz.core.selfcomp;

import com.ysy.tz.core.utils.QRCodeUtils;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import org.apache.commons.lang3.StringUtils;

public class BarCodeImageView extends ImageView {
    private StringProperty BarCodeValue = new SimpleStringProperty();

    public BarCodeImageView() {
        this.initQRPic();
    }

    public BarCodeImageView(Image image) {
        super(image);
        this.initQRPic();
    }

    private void createQRCode() {
        if (StringUtils.isNotBlank(this.getBarCodeValue()) && this.fitWidthProperty().get() != 0.0 && this.fitHeightProperty().get() != 0.0) {
            try {
                WritableImage image = QRCodeUtils.writeBarCodeToImage(this.getBarCodeValue(), (int)this.getFitWidth(), (int)this.getFitHeight());
                this.setImage(image);
            } catch (Exception var2) {
                var2.printStackTrace();
            }
        }

    }

    private void initQRPic() {
        this.BarCodeValue.addListener((observable, oldValue, newValue) -> {
            this.createQRCode();
        });
        this.fitWidthProperty().addListener((observable, oldValue, newValue) -> {
            this.createQRCode();
        });
        this.fitHeightProperty().addListener((observable, oldValue, newValue) -> {
            this.createQRCode();
        });
    }

    public String getBarCodeValue() {
        return (String)this.BarCodeValue.get();
    }

    public StringProperty barCodeValueProperty() {
        return this.BarCodeValue;
    }

    public void setBarCodeValue(String barCodeValue) {
        this.BarCodeValue.set(barCodeValue);
    }
}
